package com.qf.factory;

import com.qf.mapper.EmpMapper;
import com.qf.pojo.Emp;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.FactoryBean;

import java.io.InputStream;

/**
 * 自定义工厂Bean对象 创建
 *    表：100张
 *    Mapper接口：100个
 *    Mapper文件:100个
 *
 *    Emp dept .......
 *
 *    CommonMapper.java
 *    CommonMapper.xml  后期维护
 *      select * .... from emp
 *      select * .. from dept ....
 *
 *
 *
 *    EmpMapper.java
 *    EmpMapper.xml
 *
 *    com.qf.mapper
 *
 *    扫描   com.qf.order.mapper  com.qf.goods.mapper  com.qf.goods.service
 *    com.qf.mapper
 *
 *
 * @author lixu
 */
public class MyFactoryBean implements FactoryBean<EmpMapper> {

    /**
     * 创建返回值 EmpMapper对象的方法
     * @return
     * @throws Exception
     *
     *  <bean id="empMapper"  class="com.qf.factory.MyFactoryBean"></bean>
     */
    @Override
    public EmpMapper getObject() throws Exception {
        String resource = "mybatis-config.xml";
        InputStream resourceAsStream = Resources.getResourceAsStream(resource);
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder =
                new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        return empMapper;
    }

    @Override
    public Class<?> getObjectType() {
        return EmpMapper.class;
    }

}
